package com.basic.nio;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.util.*;

/**
 * @Description:
 * @Date: 2024-04-03 16:26
 * @Author: wz
 **/
public class SelectorTest {

    public static void main(String[] args) throws IOException {
        // 获取通道，绑定IP和端口号
        SocketChannel channel = SocketChannel.open();
        channel.connect(new InetSocketAddress("127.0.0.1",9999));
        // 设置非阻塞
        channel.configureBlocking(false);
        // 创建缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String msg = scanner.next();
            // 写入buffer数据
            buffer.put(msg.getBytes());
            // 切换模式
            buffer.flip();
            // 写入通道
            channel.write(buffer);
            // 关闭
            buffer.clear();
        }
//        //创建selector
//        Selector selector = Selector.open();
//        //创建通道
//        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
//        //非阻塞 与selector要设置非阻塞，否则报IllegalBlockingModeException
//        serverSocketChannel.configureBlocking(false);
//        //绑定连接
//        serverSocketChannel.bind(new InetSocketAddress(9999));
//        //注册到selector
//        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
//        //已经就绪的通道
//        Set<SelectionKey> selectionKeys = selector.selectedKeys();
//        Iterator<SelectionKey> iterator = selectionKeys.iterator();
//        while (iterator.hasNext()){
//            SelectionKey key = iterator.next();
//            if(key.isAcceptable()){
//
//            } else if (key.isReadable()){
//
//            } else if (key.isConnectable()) {
//
//            } else if (key.isWritable()) {
//
//            }
//            iterator.remove();
//        }
    }

    @Test
    public void serverTest() throws IOException {
        //创建selector
        Selector selector = Selector.open();
        //创建通道
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        //非阻塞 与selector要设置非阻塞，否则报IllegalBlockingModeException
        serverSocketChannel.configureBlocking(false);
        //绑定连接
        serverSocketChannel.bind(new InetSocketAddress(9999));
        //注册到selector
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        //已经就绪的通道
        while (selector.select() > 0){
            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            Iterator<SelectionKey> iterator = selectionKeys.iterator();
            while (iterator.hasNext()) {
                SelectionKey key = iterator.next();
                if(key.isAcceptable()){
                    //获取客户端连接
                    SocketChannel channel = serverSocketChannel.accept();
                    channel.configureBlocking(false);
                    //注册到选择器
                    channel.register(selector,SelectionKey.OP_READ);
                } else if (key.isReadable()){
                    SocketChannel channel = (SocketChannel)key.channel();
                    ByteBuffer buffer = ByteBuffer.allocate(1024);
                    int len;
                    while ((len = channel.read(buffer)) > 0){
                        buffer.flip();
                        String msg = new String(buffer.array(), 0, len);
                        System.out.println("客户端发来的数据："+msg.trim());
                        buffer.clear();
                    }
                }
                iterator.remove();
            }
        }
    }
    @Test
    public void clientTest() throws IOException {
        // 获取通道，绑定IP和端口号
        SocketChannel channel = SocketChannel.open();
        channel.connect(new InetSocketAddress("127.0.0.1",9999));
        // 设置非阻塞
        channel.configureBlocking(false);
        // 创建缓冲区
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()){
            String msg = scanner.next();
            // 写入buffer数据
            buffer.put(msg.getBytes());
            // 切换模式
            buffer.flip();
            // 写入通道
            channel.write(buffer);
            // 关闭通道
            channel.close();
        }

    }
}
